home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_12_01 / allison / intset.h < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-07  |  3.3 KB  |  171 lines

  1. #if !defined(INTSET_H)
  2. #define INTSET_H
  3.  
  4. #include <iostream.h>
  5. #include <stddef.h>
  6. #include "bits.h"
  7.  
  8. template<size_t N>
  9. class Intset
  10. {
  11. public:
  12.     // NOTE: The following constructors shouldn't be
  13.     // necessary. The compiler-generated ones should
  14.     // suffice. For some reason, Borland 3.1 requires
  15.     // these (WATCOM does not).
  16.  
  17.     // Constructors
  18.     Intset();
  19.     Intset(const Intset<N>& is);
  20.  
  21.     // Set operations
  22.     Intset<N> operator-(const Intset<N>& is) const;
  23.     Intset<N> operator+(const Intset<N>& is) const;
  24.     Intset<N> operator*(const Intset<N>& is) const;
  25.     Intset<N> operator~() const;
  26.     int operator==(const Intset<N>& is) const;
  27.     int operator!=(const Intset<N>& is) const;
  28.     int operator<=(const Intset<N>& is) const;
  29.     int operator>=(const Intset<N>& is) const;
  30.  
  31.     // Member operations
  32.     int contains(size_t n) const;
  33.     Intset<N>& insert(size_t n);
  34.     Intset<N>& remove(size_t n);
  35.  
  36.     size_t count() const;
  37.     friend ostream& operator<<(ostream& os, const Intset<N>& 
  38. is)
  39.       {is.print(os); return os;}
  40.  
  41. private:
  42.     bits<N> bitset;
  43.  
  44.     int subsetof(const Intset<N>& is) const;
  45.     void print(ostream& os) const;
  46. };
  47.  
  48. template<size_t N>
  49. Intset<N>::Intset()
  50. {
  51.     bitset.reset();
  52. }
  53.  
  54. template<size_t N>
  55. Intset<N>::Intset(const Intset<N>& is)
  56. {
  57.     bitset = is.bitset;
  58. }
  59.  
  60. template<size_t N>
  61. inline Intset<N> Intset<N>::operator-(const Intset<N> &is) const
  62. {
  63.     Intset<N> r(*this);
  64.     r.bitset &= ~is.bitset;
  65.     return r;
  66. }
  67.  
  68. template<size_t N>
  69. inline Intset<N> Intset<N>::operator+(const Intset<N> &is) const
  70. {
  71.     Intset<N> r(*this);
  72.     r.bitset |= is.bitset;
  73.     return r;
  74. }
  75.  
  76. template<size_t N>
  77. inline Intset<N> Intset<N>::operator*(const Intset<N> &is) const
  78. {
  79.     Intset<N> r(*this);
  80.     r.bitset &= is.bitset;
  81.     return r;
  82. }
  83.  
  84. template<size_t N>
  85. inline Intset<N> Intset<N>::operator~() const
  86. {
  87.     Intset<N> r(*this);
  88.     r.bitset.toggle();
  89.     return r;
  90. }
  91.  
  92. template<size_t N>
  93. inline int Intset<N>::operator==(const Intset<N> &is) const
  94. {
  95.     return bitset == is.bitset;
  96. }
  97.  
  98. template<size_t N>
  99. inline int Intset<N>::operator!=(const Intset<N> &is) const
  100. {
  101.     return bitset != is.bitset;
  102. }
  103.  
  104. template<size_t N>
  105. inline int Intset<N>::operator<=(const Intset<N> &is) const
  106. {
  107.     return subsetof(is);
  108. }
  109.  
  110. template<size_t N>
  111. inline int Intset<N>::operator>=(const Intset<N> &is) const
  112. {
  113.     return is.subsetof(*this);
  114. }
  115.  
  116. template<size_t N>
  117. inline int Intset<N>::contains(size_t n) const
  118. {
  119.  
  120.     return bitset.test(n);
  121. }
  122.  
  123. template<size_t N>
  124. inline Intset<N>& Intset<N>::insert(size_t n)
  125. {
  126.     bitset.set(n);
  127.     return *this;
  128. }
  129.  
  130. template<size_t N>
  131. inline Intset<N>& Intset<N>::remove(size_t n)
  132. {
  133.     bitset.reset(n);
  134.     return *this;
  135. }
  136.  
  137. template<size_t N>
  138. inline size_t Intset<N>::count() const
  139. {
  140.     return bitset.count();
  141. }
  142.  
  143. template<size_t N>
  144. inline int Intset<N>::subsetof(const Intset<N>& is) const
  145. {
  146.     bits<N> r(bitset);
  147.     r &= is.bitset;
  148.     return bitset == r;
  149. }
  150.  
  151. template<size_t N>
  152. void Intset<N>::print(ostream& os) const
  153. {
  154.     os << '{';
  155.     int first_time = 1;
  156.     for (int i = 0; i < N; ++i)
  157.         if (bitset.test(i))
  158.         {
  159.             if (!first_time)
  160.                 os << ',';
  161.             os << i;
  162.             first_time = 0;
  163.         }
  164.     os << '}';
  165. }
  166.  
  167. #endif
  168.  
  169. /* End of File */ 
  170.  
  171.